<?php

namespace App\Http\Controllers;

use App\Models\Bookmark;
use Illuminate\Http\Request;

class BookmarkController extends Controller
{
    // 显示书签列表
    public function index()
    {
        $bookmarks = Bookmark::all();
        return view('bookmarks.index', compact('bookmarks'));
    }

    // 显示添加书签表单
    public function create()
    {
        return view('bookmarks.create');
    }

    // 保存新书签
    public function store(Request $request)
    {
        $request->validate([
            'title' => 'required|string|max:255',
            'url' => 'required|url',
        ]);

        Bookmark::create($request->all());
        return redirect()->route('bookmarks.index')->with('success', '书签添加成功！');
    }

    // 显示编辑书签表单
    public function edit(Bookmark $bookmark)
    {
        return view('bookmarks.edit', compact('bookmark'));
    }

    // 更新书签
    public function update(Request $request, Bookmark $bookmark)
    {
        $request->validate([
            'title' => 'required|string|max:255',
            'url' => 'required|url',
        ]);

        $bookmark->update($request->all());
        return redirect()->route('bookmarks.index')->with('success', '书签更新成功！');
    }

    // 删除书签
    public function destroy(Bookmark $bookmark)
    {
        $bookmark->delete();
        return redirect()->route('bookmarks.index')->with('success', '书签删除成功！');
    }
}